﻿using MyabpDDD.Demo.Application.ChargeablemanagementLY.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.ChargeablemanagementLY
{
    public class ChargeablemanagementLYService : ApplicationService, IChargeablemanagementLYService
    {
        private readonly IRepository<Chargeablemanagement> _students1;
        private readonly IRepository<HeDui> _classes1;
        private readonly IRepository<JieSuanTable> _jieSuanTables;
        public ChargeablemanagementLYService(IRepository<Chargeablemanagement> students, IRepository<HeDui> classes,IRepository<JieSuanTable> jieSuanTables)
        {
            _students1 = students;
            _classes1 = classes;
            _jieSuanTables = jieSuanTables;
        }
        public async Task Delete(int id)
        {
            var list = _students1.Where(r => r.Id == id).FirstOrDefault();

            await _students1.DeleteAsync(list);
        }

        public dynamic Get(int id)
        {
            var list = _students1.Where(r => r.Id == id).ToList();
            var q2 = _classes1.AsQueryable();
            var q3 = _jieSuanTables.AsQueryable();
            var query = from a in list
                        join a2 in q2 on a.Sistrte equals a2.Id
                        join a3 in q3 on a.FangShi equals a3.Id
                        select new ChargeablemanagementLYDto()
                        {
                            Id = a.Id,
                            NumBer = a.NumBer,
                            Danwei = a.Danwei,
                            FangShi = a.FangShi,
                            DunWei = a.Danwei,
                            Price = a.Price,
                            Sum = a.Sum,
                            YeWu = a.YeWu,
                            Ren = a.Ren,
                            CreateDate = a.CreateDate,
                            BeiZhu = a.BeiZhu,
                            Sistrte=a.Sistrte,
                            HeDui=a.HeDui,
                            HeDuiDate=a.HeDuiDate,
                            Name=a3.Name,
                        };
            var yuhe = query.FirstOrDefault();
            return yuhe;
        }

        public async Task<Chargeablemanagement> Inser(ChargeablemanagementLYDto dto)
        {

            Chargeablemanagement www = ObjectMapper.Map<ChargeablemanagementLYDto, Chargeablemanagement>(dto);
            www.Sistrte = 1;
            return await _students1.InsertAsync(www);
        }

        //public dynamic QueryStudentAll()
        //{
        //    var list = _students1.AsQueryable();
        //    var q2 = _classes1.AsQueryable();
        //    var q3 = _jieSuanTables.AsQueryable();
        //    var query = from a in list
        //                join a2 in q2 on a.Sistrte equals a2.Id
        //                join a3 in q3 on a.FangShi equals a3.Id
        //                select new ChargeablemanagementLYDto()
        //                {
        //                    Id = a.Id,
        //                    NumBer = a.NumBer,
        //                    Danwei = a.Danwei,
        //                    FangShi = a.FangShi,
        //                    DunWei = a.Danwei,
        //                    Price = a.Price,
        //                    Sum = a.Sum,
        //                    YeWu = a.YeWu,
        //                    Ren = a.Ren,
        //                    CreateDate = a.CreateDate,
        //                    BeiZhu = a.BeiZhu,
        //                    Sistrte = a.Sistrte,
        //                    HeDui = a.HeDui,
        //                    HeDuiDate = a.HeDuiDate,
        //                    Name = a3.Name,
        //                    yuhName=a2.Name
        //                };
        //    return query;
        //}

        //public  dynamic QueryStudentAll(int pageIndex, int pageSiz, string huozhudanwei, int zhuangtai, string jinbanren, DateTime date, int jiesuan)
        //{
        //    var q1 = _students1.AsQueryable();
        //    var q2 = _classes1.AsQueryable();
        //    var q3 = _jieSuanTables.AsQueryable();
        //    var query = from a in q1
        //                join a2 in q2 on a.Sistrte equals a2.Id
        //                join a3 in q3 on a.FangShi equals a3.Id
        //                select new ChargeablemanagementLYDto()
        //                {
        //                    Id = a.Id,
        //                    NumBer = a.NumBer,
        //                    Danwei = a.Danwei,
        //                    FangShi = a.FangShi,
        //                    DunWei = a.Danwei,
        //                    Price = a.Price,
        //                    Sum = a.Sum,
        //                    YeWu = a.YeWu,
        //                    Ren = a.Ren,
        //                    CreateDate = a.CreateDate,
        //                    BeiZhu = a.BeiZhu,
        //                    Sistrte = a.Sistrte,
        //                    HeDui = a.HeDui,
        //                    HeDuiDate = a.HeDuiDate,
        //                    Name = a3.Name,
        //                    //yuhName = a2.Name
        //                };
        //    return query;
        //    //var list = _students1.AsQueryable();
        //    //var q2 = _classes1.AsQueryable();
        //    //var q3 = _jieSuanTables.AsQueryable();
        //    //var query = from a in list
        //    //            join a2 in q2 on a.Sistrte equals a2.Id
        //    //            join a3 in q3 on a.FangShi equals a3.Id
        //    //            select new ChargeablemanagementLYDto()
        //    //            {
        //    //                Id = a.Id,
        //    //                NumBer = a.NumBer,
        //    //                Danwei = a.Danwei,
        //    //                FangShi = a.FangShi,
        //    //                DunWei = a.Danwei,
        //    //                Price = a.Price,
        //    //                Sum = a.Sum,
        //    //                YeWu = a.YeWu,
        //    //                Ren = a.Ren,
        //    //                CreateDate = a.CreateDate,
        //    //                BeiZhu = a.BeiZhu,
        //    //                Sistrte = a.Sistrte,
        //    //                HeDui = a.HeDui,
        //    //                HeDuiDate = a.HeDuiDate,
        //    //                Name = a3.Name,
        //    //                //yuhName = a2.Name
        //    //            };
        //    //if (!string.IsNullOrEmpty(huozhudanwei))
        //    //{
        //    //    query = query.Where(x => x.Danwei == huozhudanwei);
        //    //}
        //    //if (zhuangtai > 0 && zhuangtai != null)
        //    //{
        //    //    query = query.Where(x => x.Sistrte == zhuangtai);
        //    //}
        //    //if (!string.IsNullOrEmpty(jinbanren))
        //    //{
        //    //    query = query.Where(x => x.Ren == jinbanren);
        //    //}
        //    //if (zhuangtai > 0 && zhuangtai != null)
        //    //{
        //    //    query = query.Where(x => x.Sistrte == zhuangtai);
        //    //}
        //    //var list1 = new
        //    //{
        //    //    Zongtiao = query.Count(),
        //    //    data = query.Skip((pageIndex - 1) * pageSiz).Take(pageSiz).ToList()
        //    //};
        //    //return list;



        //}

        //public dynamic QueryStudentAll(int pageIndex, int pageSiz, string huozhudanwei, int? zhuangtai, string jinbanren, DateTime? date, int? jiesuan)
        //{
        //    var list = _students1.AsQueryable();
        //    var q2 = _classes1.AsQueryable();
        //    var q3 = _jieSuanTables.AsQueryable();
        //    var query = from a in list
        //                join a2 in q2 on a.Sistrte equals a2.Id
        //                join a3 in q3 on a.FangShi equals a3.Id
        //                select new ChargeablemanagementLYDto()
        //                {
        //                    Id = a.Id,
        //                    NumBer = a.NumBer,
        //                    Danwei = a.Danwei,
        //                    FangShi = a.FangShi,
        //                    DunWei = a.Danwei,
        //                    Price = a.Price,
        //                    Sum = a.Sum,
        //                    YeWu = a.YeWu,
        //                    Ren = a.Ren,
        //                    CreateDate = a.CreateDate,
        //                    BeiZhu = a.BeiZhu,
        //                    Sistrte = a.Sistrte,
        //                    HeDui = a.HeDui,
        //                    HeDuiDate = a.HeDuiDate,
        //                    Name = a3.Name,
        //                    //yuhName = a2.Name
        //                };
        //    if (!string.IsNullOrEmpty(huozhudanwei))
        //    {
        //        query = query.Where(x => x.Danwei == huozhudanwei);
        //    }
        //    if (zhuangtai > 0 && zhuangtai != null)
        //    {
        //        query = query.Where(x => x.Sistrte == zhuangtai);
        //    }
        //    if (!string.IsNullOrEmpty(jinbanren))
        //    {
        //        query = query.Where(x => x.Ren == jinbanren);
        //    }
        //    if (zhuangtai > 0 && zhuangtai != null)
        //    {
        //        query = query.Where(x => x.Sistrte == zhuangtai);
        //    }
        //    var list1 = new
        //    {
        //        Zongtiao = query.Count(),
        //        data = query.Skip((pageIndex - 1) * pageSiz).Take(pageSiz).ToList()
        //    };
        //    return list;
        //}

        public bool SelectDel(string ids)
        {
            string[] int_ids = ids.Split(',');
            var q = from id in int_ids
                    select Convert.ToInt32(id);
            var q_list = q.ToList();
            var query = _students1.AsQueryable();
            query = query.Where(d => q_list.Contains
              (d.Id));
            foreach (var model in query)
            {
                _students1.DeleteAsync(model);
            }
            return true;
        }

        public async Task<bool> update(ChargeablemanagementLYDto dto)
        {
            Chargeablemanagement www = ObjectMapper.Map<ChargeablemanagementLYDto, Chargeablemanagement>(dto);
            var db = _students1.GetDbSet<Chargeablemanagement>();
            db.Update(www);
            return true;
        }
    }
}
